<template>
  <div class="article">
    <el-card shadow="never" class="box-card margin-30">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row :gutter="200">
          <el-col :span="6">
            <el-form-item label="作者">
              <el-input v-model="form.author" placeholder="作者" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题">
              <el-input v-model="form.title" placeholder="标题" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->

            <el-form-item label="编辑器类型">
              <!-- <el-input v-model="form.editorType" /> -->
              <el-select v-model="form.editorType" placeholder="编辑器类型" clearable>
                <el-option
                  label="富文本"
                  :value="0"
                />
                <el-option
                  label="markdown"
                  :value="1"
                />
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="18">
            <el-button type="default" size="mini" @click="resetForm">重置</el-button>
            <el-button type="primary" size="mini" @click="doSearch">搜索</el-button>

          </el-col>

        </el-row>

      </el-form>

    </el-card>

    <!-- 内容卡片 -->
    <el-card shadow="hover" class="margin-30">
      <div slot="header">
        <el-button type="primary" size="mini" @click="goAddArticle">新增</el-button>

      </div>
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        :data="articleList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          prop="title"
          label="文章标题"
          width="300"
          align="center"
        />
        <el-table-column
          label="图片展示"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <img width="80" height="80" :src="scope.row.coverImg" alt="coverImg">
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          width="300"
          align="center"
        />
        <el-table-column
          prop="author"
          label="文章作者"
          width="200"
          align="center"
        />
        <el-table-column
          prop="isShow"
          label="文章是否显示"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <!-- isShow (integer, optional): 是否显示: 0 否; 1 是 , -->
            <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0" @change="articleStatusChange(scope.row.id,$event)" />

          </template>
        </el-table-column>
        <el-table-column
          prop="editorType"
          label="编辑类型"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->
            <el-button v-if="scope.row.editorType===0" plain type="primary" size="mini"> 富文本 </el-button>
            <el-button v-if="scope.row.editorType===1" plain type="success" size="mini"> markdown </el-button>

          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-view" @click="goEditArticle(scope.row.id)">编辑文章</el-button>
            <el-button type="text" style="color:red;" size="mini" @click="doDeleteArticleById(scope.row.id)">删除</el-button>

          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->

      <el-pagination
        style="text-align:center;margin-top:20px;"
        :current-page="paginationObj.start"
        :page-sizes="paginationObj.pageSizes"
        :page-size="paginationObj.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginationObj.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>
<script>
import {
  findArticles as findArticlesApi,
  changeShowStatus as changeShowStatusApi,
  deleteArtilce as deleteArtilceApi
} from '@/api/content/article'
import { filterNullParams } from '@/utils'
import mix from '@/mixins/index'
export default {
  name: 'Article',
  mixins: [mix],
  data() {
    return {
      loading: false,
      articleList: [],

      form: {}

    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    // console.log(123)
    this.getArticleList()
  },
  mounted() {
  },
  methods: {
    // 编辑文章
    goEditArticle(id) {
      // this.$router.push({ name: 'ArticleDetail', query: { id }})
      this.$router.push({ name: 'ArticleDetail', params: { id }})
    },
    // 新增
    goAddArticle() {
      this.$router.push({ name: 'ArticleDetail' })
    },
    resetForm() {
      // console.log(11)
      // this.$refs.form.resetFields()
      this.form = {}
      // console.log(22)
    },
    doSearch() {
      this.paginationObj.start = 1
      this.getArticleList()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.paginationObj.limit = val
      this.paginationObj.start = 1
      this.getArticleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.paginationObj.start = val
      this.getArticleList()
    },
    async articleStatusChange(id, isShow) {
      // console.log(id, isShow)
      const res = await changeShowStatusApi({
        isShow,
        id
      })
      const { success: succ, message } = res
      if (succ) {
        this.$message.success('修改状态成功')
      } else {
        this.$message.error(message)
      }
      // console.log(res)
    },
    async getArticleList() {
      const newObj = filterNullParams(this.form)
      // 开启进度条
      this.loading = true
      const res = await findArticlesApi(this.paginationObj.start, this.paginationObj.limit, newObj)
      // console.log(res)
      // 关闭进度条
      this.loading = false
      const { success, data, message } = res
      if (success) {
        const { rows, total } = data
        this.articleList = rows
        this.paginationObj.total = total
      } else {
        this.$message.error(message)
      }
    },
    // 文章删除
    doDeleteArticleById(id) {
      // console.log('id==>', id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确认
        deleteArtilceApi(id)
          .then(res => {
            const { success, message } = res
            if (success) {
              this.$message.success('文章删除成功')
              // 刷新数据
              this.getArticleList()
            } else {
              this.$message.error(message)
            }
          })
      }).catch(() => {
        // 点击取消
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.article{

}
</style>
